import { Injectable } from '@angular/core';
import { ActivatedRouteSnapshot, CanActivate, RouterStateSnapshot, UrlTree } from '@angular/router';
import { Observable } from 'rxjs';

@Injectable({
  providedIn: 'root'
})
export class MyGuardService implements CanActivate {
  private componentState: boolean = false;

  canActivate(
    next: ActivatedRouteSnapshot,
    state: RouterStateSnapshot): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
    if (this.componentState) {
      return false;
    } else {
      return true;
      // Redirect to the home page or some other page
    }
  }

  public setComponentState(state: boolean) {
    this.componentState = state;
  }
}
